package com.dp.temp;

import com.dp.io.ESN;
import com.dp.logUtil.SetMsg;
import com.dp.poi.ReadAndCreateExcel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@RestController
@RequestMapping("/createRiskControl")
public class createRiskControl {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping(value = "/wordHdPcRw")
    public void wordHdPcRw(HttpServletResponse response) {
        try {
            Workbook workbook = hdPcRw2(wordHdXh(), "document/createRiskControl.xlsx");
            OutputStream out = response.getOutputStream();
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=风险管控报告.xlsx");
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public List<Map<String, Object>> wordHdXh() {
        String sql = "SELECT" +
                "\trc.RID,\n" +
                "\trc.RISK_EVENT_ID,\n" +
                "\tre.RISK_EVENT_NAME,\n" +
                "\tru.RISK_UNIT_NAME,\n" +
                "\tro.ONAME,\n" +
                "\tro.HAZARD_DEP,\n" +
                "\tro.HAZARD_LIABLE_PERSON,\n" +
                "\trc.ATA_SRC,\n" +
                "\trc.CLASSIFY1,\n" +
                "\trc.CLASSIFY2,\n" +
                "\trc.CLASSIFY3,\n" +
                "\trc.CCONTENT,\n" +
                "\trc.TROUBLESHOOT_CONTENT,\n" +
                "\trc.CREATE_DATE,\n" +
                "\trc.CREATE_BY,\n" +
                "\trc.CREATE_BY_ID,\n" +
                "\trc.UPDATE_DATE,\n" +
                "\trc.UPDATE_BY,\n" +
                "\trc.POST_LEADER,\n" +
                "\trc.CYCLE,\n" +
                "\trc.CYCLE_UNIT, \n" +
                "\trc.WORK_DAY_TYPE, \n" +
                "\trc.WORK_TYPE, \n" +
                "\trc.TASK_NUM, \n" +
                "\trc.CREMARK \n" +
                "FROM\n" +
                "\trisk_control rc LEFT\n" +
                "\tJOIN risk_event re ON rc.RISK_EVENT_ID = re.RISK_EVENT_ID LEFT\n" +
                "\tJOIN risk_unit ru ON re.RISK_UNIT_ID = ru.RISK_UNIT_ID LEFT\n" +
                "\tJOIN risk_object ro ON ru.OID = ro.OID \n" +
                "WHERE\n" +
                "\trc.DELETED = '0'\n" +
                "\tAND re.DELETED = '0'\n" +
                "\tAND ru.DELETED = '0' \n" +
                "\tAND ro.DELETED = '0'  ORDER BY ro.ONAME,ro.HAZARD_DEP,ro.HAZARD_LIABLE_PERSON,ru.RISK_UNIT_NAME," +
                "re.RISK_EVENT_NAME,rc.CLASSIFY1,rc.CLASSIFY2,rc.CLASSIFY3,rc.CCONTENT," +
                "rc.TROUBLESHOOT_CONTENT";
        return jdbcTemplate.queryForList(sql);
    }


    public static Workbook hdPcRw2(List<Map<String, Object>> map, String path) throws IOException {
        Resource resource = new ClassPathResource(path);
        Workbook workbook = new XSSFWorkbook(resource.getInputStream());
        int count = 5;
        int rcount = 4;
        for (int i = 0; i < map.size(); i++) {
            if (i != map.size() - 1) {
                ReadAndCreateExcel.copyRows(5, 5, count, ((XSSFWorkbook) workbook).getSheetAt(0));
            }
            Row row = workbook.getSheetAt(0).getRow(rcount);
            if (row != null) {
                row.getCell(0).setCellValue(map.get(i).get("ONAME").toString());
                row.getCell(1).setCellValue(map.get(i).get("HAZARD_DEP").toString());
                row.getCell(2).setCellValue(map.get(i).get("HAZARD_LIABLE_PERSON").toString());
                row.getCell(3).setCellValue(map.get(i).get("RISK_UNIT_NAME").toString());
                row.getCell(4).setCellValue(map.get(i).get("RISK_EVENT_NAME").toString());
                if (Integer.parseInt(map.get(i).get("ATA_SRC").toString()) == 1) {
                    row.getCell(5).setCellValue("自动化监控");
                } else {
                    row.getCell(5).setCellValue("隐患排查");
                }
                switch (Integer.parseInt(map.get(i).get("CLASSIFY1").toString())){
                    case 1:
                        row.getCell(6).setCellValue("工程技术");
                        break;
                    case 2:
                        row.getCell(6).setCellValue("维护保养");
                        break;
                    case 3:
                        row.getCell(6).setCellValue("操作行为");
                        break;
                    case 4:
                        row.getCell(6).setCellValue("应急措施");
                        break;
                }
                switch (map.get(i).get("CLASSIFY2").toString()){
                    case "1-1":
                        row.getCell(7).setCellValue("工艺控制");
                        break;
                    case "1-2":
                        row.getCell(7).setCellValue("关键设备/部件");
                        break;
                    case "1-3":
                        row.getCell(7).setCellValue("安全附件");
                        break;
                    case "1-4":
                        row.getCell(7).setCellValue("安全仪表");
                        break;
                    case "1-5":
                        row.getCell(7).setCellValue("其它");
                        break;
                    case "2-1":
                        row.getCell(7).setCellValue("动设备");
                        break;
                    case "2-2":
                        row.getCell(7).setCellValue("静设备");
                        break;
                    case "3-1":
                        row.getCell(7).setCellValue("人员资质");
                        break;
                    case "3-2":
                        row.getCell(7).setCellValue("操作记录");
                        break;
                    case "3-3":
                        row.getCell(7).setCellValue("交接班");
                        break;
                    case "4-1":
                        row.getCell(7).setCellValue("应急设施");
                        break;
                    case "4-2":
                        row.getCell(7).setCellValue("个体防护");
                        break;
                    case "4-3":
                        row.getCell(7).setCellValue("消防设施");
                        break;
                    case "4-4":
                        row.getCell(7).setCellValue("应急预案");
                        break;
                }
                row.getCell(8).setCellValue(map.get(i).get("CLASSIFY3").toString());
                row.getCell(9).setCellValue(map.get(i).get("CCONTENT").toString());
                row.getCell(10).setCellValue(map.get(i).get("TROUBLESHOOT_CONTENT").toString());
                row.getCell(11).setCellValue(map.get(i).get("CYCLE").toString());
                row.getCell(12).setCellValue(map.get(i).get("CYCLE_UNIT").toString());
                switch (Integer.parseInt(map.get(i).get("CREMARK").toString())){
                    case 1:
                        row.getCell(13).setCellValue("重大风险");
                        break;
                    case 2:
                        row.getCell(13).setCellValue("较大风险");
                        break;
                    case 3:
                        row.getCell(13).setCellValue("一般风险");
                        break;
                    case 4:
                        row.getCell(13).setCellValue("低风险");
                        break;
                }
                switch (Integer.parseInt(map.get(i).get("WORK_DAY_TYPE").toString())){
                    case 1:
                        row.getCell(14).setCellValue("法定工作日");
                        break;
                    case 2:
                        row.getCell(14).setCellValue("非法定工作日");
                        break;
                    case 0:
                        row.getCell(14).setCellValue("每天");
                        break;
                }
                switch (Integer.parseInt(map.get(i).get("WORK_TYPE").toString())){
                    case 1:
                        row.getCell(15).setCellValue("主要负责人任务");
                        break;
                    case 2:
                        row.getCell(15).setCellValue("技术负责人任务");
                        break;
                    case 3:
                        row.getCell(15).setCellValue("操作负责人任务");
                        break;
                    case 0:
                        row.getCell(15).setCellValue("日常任务");
                        break;
                }
                if ("0".equals(map.get(i).get("WORK_TYPE").toString())){
                    row.getCell(15).setCellValue("否");
                }else {
                    row.getCell(15).setCellValue("是");
                }
            }
            rcount++;
            count++;
        }
        return workbook;
    }

}
